package LeetCode.month02.day54;

import java.util.Arrays;

public class No1040 {
    public int[] numMovesStonesII(int[] stones) {
        int n = stones.length;
        Arrays.sort(stones);
        int e1 = stones[n-2]-stones[0]-n+2;
        int e2 = stones[n-1]-stones[1]-n+2;
        int max = Math.max(e1,e2);
        if(e1==0||e2==0){
            return new int[]{Math.min(2,max),max};
        }
        int left = 0;
        int maxcount = 0;
        for (int right = 0; right <n; right++) {
            while(stones[right]-stones[left]>n){
                left++;
            }
            maxcount = Math.max(maxcount,right-left+1);
        }
        return new int[]{n-maxcount,max};
    }
}
